//
//  SAAnimationManager.swift
//  SA
//
//  Created by mac on 2022/12/6.
//

import UIKit
import SwiftEntryKit

struct RaeAnimationWarpper<AnimationEntry> {
    let animation: AnimationEntry
    init(_ animation: AnimationEntry) {
        self.animation = animation
    }
}

protocol RaeAnimationProtocol { }
extension RaeAnimationProtocol {
    
    var entryAnimate: RaeAnimationWarpper<Self> {
        get { return RaeAnimationWarpper(self) }
        set {  }
    }
    
}

extension RaeAnimationWarpper {
    /// 是否有视图正在显示中
    func isViewDisplaying() -> Bool {
        SwiftEntryKit.isCurrentlyDisplaying
    }
    
    func animation(show delegate: RaeAnimationDelegate?) {
        // 收起键盘
        UIApplication.window?.endEditing(true)
        
        var entryName = ""
        var containerSize = delegate?.animationContainerViewSize() ?? .zero
        if let containerView = delegate as? UIView {
            if containerSize.isZero {
                containerSize = containerView.frame.size
            }
            entryName = NSStringFromClass(containerView.classForCoder)
        }
        if let controller = delegate as? UIViewController {
            if containerSize.isZero {
                containerSize = controller.view.frame.size
            }
            entryName = NSStringFromClass(controller.classForCoder)
        }
        
        var attributes: EKAttributes = EKAttributes.default
        let viewStyle = delegate?.animationContainerViewStyle() ?? .center
        if viewStyle == .center {
            attributes = EKAttributes.centerFloat
        } else if viewStyle == .bottom {
            attributes = EKAttributes.bottomFloat
            // 安全边距
            attributes.positionConstraints.safeArea = .overridden
            attributes.positionConstraints.verticalOffset = 0
        }
        attributes.screenInteraction = delegate?.animationContainerScreenInteraction() ?? .dismiss
        attributes.scroll = delegate?.animationContainerScroll() ?? .disabled
        

        attributes.name = entryName
        attributes.displayDuration = .infinity
        attributes.screenBackground = .color(color: .black.with(alpha: 0.4))
        attributes.shadow = .active(
            with: .init(
                color: .black,
                opacity: 0.3,
                radius: 8
            )
        )
        attributes.entranceAnimation = .init(
            translate: .init(
                duration: 0.7,
                spring: .init(damping: 0.7, initialVelocity: 0)
            ),
            scale: .init(
                from: 0.7,
                to: 1,
                duration: 0.4,
                spring: .init(damping: 1, initialVelocity: 0)
            )
        )
        attributes.entryInteraction = .absorbTouches
        attributes.exitAnimation = .init(translate: .init(duration: 0.2))
        attributes.popBehavior = .animated(animation: .init(translate: .init(duration: 0.35)))
        attributes.positionConstraints.size = .init(
            width: .constant(value: containerSize.width),
            height: .constant(value: containerSize.height)
        )
        // 自定义样式
        attributes = delegate?.animation(customAttributes: attributes) ?? attributes
        
        if let containerView = delegate as? UIView {
            SwiftEntryKit.display(entry: containerView, using: attributes)
        }
        if let controller = delegate as? UIViewController {
            SwiftEntryKit.display(entry: controller, using: attributes)
        }
    }
    
    func animation(dismiss delegate: RaeAnimationDelegate?, completion: (() -> Void)? = nil) {
        var entryName = ""
        if let containerView = delegate as? UIView {
            entryName = NSStringFromClass(containerView.classForCoder)
        }
        if let controller = delegate as? UIViewController {
            entryName = NSStringFromClass(controller.classForCoder)
        }
        SwiftEntryKit.dismiss(entryName.isEmpty ? .all : .specific(entryName: entryName), with: completion)
    }
    
}
